﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.Client;

namespace Election.ToTheoDoiKetQua
{
    public partial class ToTheoDoiKetQua : Form
    {
        public ToTheoDoiKetQua()
        {
            InitializeComponent();
        }

        public class Item
        {
            private string _text;
            private string _value;
            private bool _isReadOnly = false;
            public string Text
            {
                get
                {
                    return this._text;
                }
                set { this._text = value; }
            }
            public string Value
            {
                get { return this._value; }
                set { this._value = value; }
            }
            public bool isReadOnly
            {
                get { return this._isReadOnly; }
                set { this._isReadOnly = value; }
            }

            public Item()
            {

            }
            public Item(string text, string value)
            {
                this.Value = value;
                this.Text = text;
            }
            public Item(string text, string value, bool readOnly)
            {
                this.Value = value;
                this.Text = text;
                this.isReadOnly = readOnly;
            }
            public override string ToString()
            {
                return this.Text;
            }
        }        
        
        private void formShown(){
            string query = "select MADV, TENDV from quanlybaucu.donvi";
            try
            {
                DataTable dataTbl = orcl.ExecuteQueryThrowEx(query);
                this.criteriaCmbBox.Items.Add(new Item("Tất cả", ""));
                for (int i = 0; i < dataTbl.Rows.Count; i++)
                {
                    this.criteriaCmbBox.Items.Add(new Item(dataTbl.Rows[i]["TENDV"].ToString(), dataTbl.Rows[i]["MADV"].ToString()));
                }
                
                query = "select MADOTBAUCU from quanlybaucu.dotbaucu";
                dataTbl = orcl.ExecuteQueryThrowEx(query);
                if (dataTbl.Rows.Count == 0)
                {
                    MessageBox.Show("Không có thông tin đợt bầu cử nào trong hệ thống");
                    return;
                }
                for (int i = 0; i < dataTbl.Rows.Count; i++)
                {
                    this.dotBauCuCmbBox.Items.Add(dataTbl.Rows[i]["MADOTBAUCU"].ToString());
                }
                this.dotBauCuCmbBox.SelectedIndex = 0;
                this.criteriaCmbBox.SelectedIndex = 0;
            }
            catch (System.Exception ex)
            {
                if (ex.GetType().Name.CompareTo("OracleException") == 0)
                {
                    OracleException oraEx = (OracleException)ex;
                    switch (oraEx.Number)
                    {
                        case 942:
                            MessageBox.Show("Bạn không có quyền truy cập thông tin này");
                            return;
                        default:
                            MessageBox.Show(oraEx.Message);
                            return;
                    }
                }
                else
                    MessageBox.Show(ex.Message);
            }
            query = "select * from table(quanlybaucu.func_theoDoiThongTinNBB('" + this.dotBauCuCmbBox.Items[this.dotBauCuCmbBox.SelectedIndex].ToString() + "')) ";
            string[] headerArr = new String[] { "Mã Người Bình Bầu", "Mã Thành Viên", "Họ Và Tên", "Tình Trạng Bầu Cử", "Phái", "Thuộc Đơn Vị" };
            try
            {
                DataTable dataTbl = orcl.ExecuteQueryThrowEx(query);
                this.reportGridView.DataSource = dataTbl;
                for (int i = 0; i < headerArr.Length; i++)
                {
                    this.reportGridView.Columns[i].HeaderText = headerArr[i];
                    this.reportGridView.Columns[i].Width = 150;
                    this.reportGridView.Columns[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                }
                query = " select * from table (quanlybaucu.func_tinhTongSoNguoiDaBau('" + this.dotBauCuCmbBox.Items[this.dotBauCuCmbBox.SelectedIndex].ToString() + "')) ";
                dataTbl = orcl.ExecuteQueryThrowEx(query);
                if (dataTbl.Rows.Count == 0)
                {
                    this.votedNumLbl.Text = "Unavailable";
                    this.unvotedNumLbl.Text = "Unavailable";
                }
                else
                {
                    this.votedNumLbl.Text = dataTbl.Rows[0]["SOLUONG"].ToString();
                    query = "select count(*) as SOLUONG from quanlybaucu.nguoibinhbau where madotbaucu = '" + this.dotBauCuCmbBox.Items[this.dotBauCuCmbBox.SelectedIndex].ToString() + "' ";
                    dataTbl = orcl.ExecuteQueryThrowEx(query);
                    this.unvotedNumLbl.Text = (int.Parse(dataTbl.Rows[0]["SOLUONG"].ToString()) - int.Parse(this.votedNumLbl.Text)).ToString();
                }
                
            }
            catch (System.Exception ex)
            {

                if (ex.GetType().Name.CompareTo("OracleException") == 0)
                {
                    OracleException oraEx = (OracleException)ex;
                    switch (oraEx.Number)
                    {
                        case 942:
                            MessageBox.Show("Bạn không có quyền truy cập thông tin này");
                            return;
                        default:
                            MessageBox.Show(oraEx.Message);
                            return;
                    }
                }
                else
                    MessageBox.Show(ex.Message);
            }
        }

        public void thongKetNguoiBinhBau()
        {
            try
            {
                string query = "select * from table(quanlybaucu.func_theoDoiThongTinNBB('" + this.dotBauCuCmbBox.Items[this.dotBauCuCmbBox.SelectedIndex].ToString() + "')) ";
                string query2 = " select * from table (quanlybaucu.func_tinhTongSoNguoiDaBau('" + this.dotBauCuCmbBox.Items[this.dotBauCuCmbBox.SelectedIndex].ToString() + "')) ";
                string query3 = " select count(*) as SOLUONG from quanlybaucu.nguoibinhbau where madotbaucu = '" + this.dotBauCuCmbBox.Items[this.dotBauCuCmbBox.SelectedIndex].ToString() + "' ";
                Item item = new Item("", "");
                if(this.criteriaCmbBox.SelectedItem != null)
                    item = (Item)this.criteriaCmbBox.SelectedItem;
                if (item.Value != "")
                {
                    query = "select * from table(quanlybaucu.func_theoDoiThongTinNBBTheoDV('" + this.dotBauCuCmbBox.Items[this.dotBauCuCmbBox.SelectedIndex].ToString() + "' ,'" + item.Value + "')) ";
                    query2 = "select * from table(quanlybaucu.func_tinhTongSoDaBauTheoDonVi('" + this.dotBauCuCmbBox.Items[this.dotBauCuCmbBox.SelectedIndex].ToString() + "' ,'" + item.Value + "')) ";
                    query3 = "select * from table(quanlybaucu.func_tinhTongNBBTheoDonVi('" + this.dotBauCuCmbBox.Items[this.dotBauCuCmbBox.SelectedIndex].ToString() + "' ,'" + item.Value + "')) "; ;
                }
                string[] headerArr = new String[] { "Mã Người Bình Bầu", "Mã Thành Viên", "Họ Và Tên", "Tình Trạng Bầu Cử", "Phái", "Thuộc Đơn Vị" };
            
                DataTable dataTbl = orcl.ExecuteQueryThrowEx(query);
                this.reportGridView.DataSource = dataTbl;
                for (int i = 0; i < headerArr.Length; i++)
                {
                    this.reportGridView.Columns[i].HeaderText = headerArr[i];
                    this.reportGridView.Columns[i].Width = 150;
                    this.reportGridView.Columns[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                }
                                
                dataTbl = orcl.ExecuteQueryThrowEx(query2);
                if (dataTbl.Rows.Count == 0)
                {
                    this.votedNumLbl.Text = "Unavailable";
                    this.unvotedNumLbl.Text = "Unavailable";
                }
                else
                {
                    if (dataTbl.Rows[0]["SOLUONG"].ToString() == "")
                        this.votedNumLbl.Text = "0";
                    else
                        this.votedNumLbl.Text = dataTbl.Rows[0]["SOLUONG"].ToString();
                    dataTbl = orcl.ExecuteQueryThrowEx(query3);
                    this.unvotedNumLbl.Text = (int.Parse(dataTbl.Rows[0]["SOLUONG"].ToString()) - int.Parse(this.votedNumLbl.Text)).ToString();
                }

            }
            catch (System.Exception ex)
            {
                if (ex.GetType().Name.CompareTo("OracleException") == 0)
                {
                    OracleException oraEx = (OracleException)ex;
                    switch (oraEx.Number)
                    {
                        case 942:
                            MessageBox.Show("Bạn không có quyền truy cập thông tin này");
                            return;
                        default:
                            MessageBox.Show(oraEx.Message);
                            return;
                    }
                }
                else
                    MessageBox.Show(ex.Message);
            }            
        }

        private void dotBauCuCmbBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.dotBauCuCmbBox.SelectedItem !=null)
                this.thongKetNguoiBinhBau();
        }

        private void criteriaCmbBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.criteriaCmbBox.SelectedItem != null)
                this.thongKetNguoiBinhBau();
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {
            this.dotBauCuCmbBox.Items.Clear();
            this.criteriaCmbBox.Items.Clear();
            this.formShown();
        }

        private void dotBauCuCmbBox_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            this.thongKetNguoiBinhBau();
        }

        private void criteriaCmbBox_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            this.thongKetNguoiBinhBau();
        }

        private void ToTheoDoiKetQua_Load(object sender, EventArgs e)
        {
            this.formShown();
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {
                    
        }

        public void loadDotBauCu(String maDotBauCu)
        {
            try
            {
                //CHECK HERE - thieu hinh anh
                //get info of ungcuvien
                string query = "select * from table(quanlybaucu.func_thongKeKetQuaBB('" + maDotBauCu + "')) ";
                DataTable dataTbl = orcl.ExecuteQueryThrowEx(query);
                UngCuVienUC[] ucArr = new UngCuVienUC[] { this.ungCuVienUC1, this.ungCuVienUC2, this.ungCuVienUC3, this.ungCuVienUC4, this.ungCuVienUC5 };
                //check if info is available
                if (dataTbl.Rows.Count == 0)
                {
                    for (int i = 0; i < 5; i++)
                    {
                        ucArr[i].setUngCuVienInfo("UNAVAILABLE", "", 0, "");
                    }
                    MessageBox.Show("Thông tin đợt bầu cử này chưa có trong hệ thống");
                    return;
                }

                // set each ucv info for ucv user control                
                for (int i = 0; i < 5; i++)
                {
                    if (i < dataTbl.Rows.Count)
                        ucArr[i].setUngCuVienInfo(dataTbl.Rows[i]["MAUCV"].ToString(), dataTbl.Rows[i]["TEN"].ToString(), int.Parse(dataTbl.Rows[i]["SOLUONG"].ToString()), dataTbl.Rows[i]["MAUCV"].ToString());
                    else
                        ucArr[i].setUngCuVienInfo("UNAVAILABLE", "", 0, "");
                }

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void dotBauCuCmbBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.dotBauCuCmbBox2.SelectedItem == null)
                return;
            string maDotBauCu = this.dotBauCuCmbBox2.Items[this.dotBauCuCmbBox2.SelectedIndex].ToString();
            loadDotBauCu(maDotBauCu);
        }

        private void refreshBut_Click(object sender, EventArgs e)
        {
            if (this.dotBauCuCmbBox2.SelectedItem == null)
                return;
            string maDotBauCu = this.dotBauCuCmbBox2.Items[this.dotBauCuCmbBox.SelectedIndex].ToString();
            loadDotBauCu(maDotBauCu);
        }

        private void tabControl1_TabIndexChanged(object sender, EventArgs e)
        {
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.dotBauCuCmbBox2.Items.Clear();
            if (this.tabControl1.SelectedIndex == 1)
            {
                try
                {
                    //lay thong tin dot bau cu
                    string query = "select MADOTBAUCU from quanlybaucu.dotbaucu";
                    DataTable dataTbl = orcl.ExecuteQueryThrowEx(query);
                    if (dataTbl.Rows.Count == 0)
                    {
                        MessageBox.Show("Không có thông tin đợt bầu cử nào trong hệ thống");
                        return;
                    }
                    for (int i = 0; i < dataTbl.Rows.Count; i++)
                    {
                        this.dotBauCuCmbBox2.Items.Add(dataTbl.Rows[i]["MADOTBAUCU"].ToString());
                    }
                    this.dotBauCuCmbBox2.SelectedIndex = 0;
                    loadDotBauCu(dataTbl.Rows[0]["MADOTBAUCU"].ToString());
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

    }
}
